package chapter08

/**
  * 匹配对象
  */
object Test04_MatchObject {
  def main(args: Array[String]): Unit = {
    val yi = new Student("yi", 20)
    // 针对对象实例的内容进行匹配
    val result: String = yi match {
      case Student("yi", 20) => "yi 20"
      case _ => "else people"
    }
    println(result)
  }
}


class Student(val name: String, val age: Int)

object Student {
  def apply(name: String, age: Int): Student = new Student(name, age)


  // 必须实现一个unapply方法，用来对对象属性进行拆解
  def unapply(arg: Student): Option[(String, Int)] = {
    if (arg == null) {
      None
    } else {
      Some(arg.name, arg.age)
    }
  }
}
